package org.csy.work.servlet;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.csy.work.dao.ProductDao;
import org.csy.work.dao.impl.ProductDaoImpl;
import org.csy.work.entity.Product;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author CSY
 * @date 2024-11-28
 */
@WebServlet("/load.do")
public class LoadInfoServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        ProductDao productDao = new ProductDaoImpl();
        List<Product> list = productDao.getProductAll();

        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String json = gson.toJson(list);

        PrintWriter out = resp.getWriter();
        out.print(json);

        out.flush();
        out.close();

    }
}
